package com.hr.controller;

import com.hr.dto.JsonResp;
import com.hr.model.*;
import com.hr.service.ArchiveService;
import com.hr.service.DeedService;
import com.hr.service.TalentService;
import com.hr.service.UserService;
import com.hr.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/emp")
public class EmpController
{
    @Autowired
    ArchiveService archiveService;
    @Autowired
    DeedService deedService;
    @Autowired
    TalentService talentService;

    /**
     * 个人档案
     */
    @RequestMapping(value = "/file/get",method = RequestMethod.GET)
    ResponseEntity<JsonResp> getArchive()
    {
        JsonResp jsonResp;
        Archive archive=archiveService.getArchiveByAId(AuthUtil.getCurUser().getId());
        if(archive==null)
            jsonResp=JsonResp.ok("尚未创建档案");
        else
        {
            for(Evaluate evaluate: archive.getEvaluates())
            {
                if(evaluate.getCommenter()==null||evaluate.getCommenter().getArchive()==null)
                    continue;
                evaluate.getCommenter().getArchive().cutTail();
            }
            jsonResp=JsonResp.ok("archive",archive);
        }

        return new ResponseEntity<>(jsonResp, HttpStatus.OK);
    }

    /**
     * 个人职位
     */
    @RequestMapping(value = "/pos/get", method = RequestMethod.GET)
    ResponseEntity<JsonResp> getPositions()
    {
        JsonResp jsonResp;
        Archive archive=((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getArchive();
        if(archive==null)
            jsonResp=JsonResp.ok("尚未创建档案");
        else if(archive.getPositions().size()==0)
            jsonResp=JsonResp.ok("尚无职位");
        else
            jsonResp=JsonResp.ok("positions",archive.getPositions());
        return new ResponseEntity<>(jsonResp, HttpStatus.OK);
    }

    /**
     * 个人事迹
     */
    @RequestMapping(value = "/deed/get", method = RequestMethod.GET)
    ResponseEntity<JsonResp> getDeeds()
    {
        JsonResp jsonResp;
        Archive archive=((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getArchive();
        if(archive==null)
            jsonResp=JsonResp.ok("尚未创建档案");
        else if(archive.getDeeds().size()==0)
            jsonResp=JsonResp.ok("尚无事迹");
        else
            jsonResp=JsonResp.ok("deeds",archive.getDeeds());
        return new ResponseEntity<>(jsonResp, HttpStatus.OK);
    }

    /**
     * 新建档案
     */
    @RequestMapping(value = "/file/post", method = RequestMethod.POST)
    ResponseEntity<JsonResp> postArchive(@RequestBody Archive archive) throws Exception
    {
        User user=(User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user.getArchive()!=null)
            throw new Exception("档案已存在");

        archiveService.insertArchiveByUId(user.getId(),archive);
        user.setArchive(archiveService.getArchiveByUId(user.getId()));

        return new ResponseEntity<>(JsonResp.ok("创建成功","aid",archive.getId()), HttpStatus.OK);
    }

    /**
     * 修改档案
     */
    @RequestMapping(value = "/file/put", method = RequestMethod.PUT)
    ResponseEntity<JsonResp> putArchive(@RequestBody Archive archive) throws Exception
    {
        User user=(User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user.getArchive()==null)
            throw new Exception("尚未创建档案");

        archive.setDate(new Date());//更新修改时间
        archiveService.updateArchiveByAId(user.getArchive().getId(),archive);

        user.setArchive(archiveService.getArchiveByUId(user.getId()));

        return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
    }

    /**
     * 新增事迹
     */
    @RequestMapping(value = "/deed/post", method = RequestMethod.POST)
    ResponseEntity<JsonResp> postDeed(@RequestBody Deed deed) throws Exception
    {
        Archive archive=AuthUtil.getCurUser().getArchive();
        if(archive==null)
            throw new Exception("尚未创建档案");

        //验证职位属于自己
        boolean avai=false;

        if(deed.getPosition()==null)
            avai=true;
        else
        {
            for(Position position: archive.getPositions())
            {
                if(position.getId()==deed.getPosition().getId())
                {
                    avai=true;
                    break;
                }
            }
            if(!avai)
                return new ResponseEntity<>(JsonResp.badRequest("该职位归属他人或不存在"), HttpStatus.BAD_REQUEST);
        }

        deedService.insertDeedByAId(deed);
        archive.setDeeds(deedService.getDeedsByAId(archive.getId()));

        return new ResponseEntity<>(JsonResp.ok("新增成功","deeid",deed.getId()), HttpStatus.OK);
    }

    /**
     * 修改事迹
     */
    @RequestMapping(value = "/deed/put", method = RequestMethod.PUT)
    ResponseEntity<JsonResp> putDeed(@RequestBody Deed deed) throws Exception
    {
        Archive archive= AuthUtil.getCurUser().getArchive();
        if(archive==null)
            throw new Exception("尚未创建档案");

        //验证职位属于自己
        boolean avai=false;

        if(deed.getPosition()==null)
            avai=true;
        else
        {
            for(Position position: archive.getPositions())
            {
                if(position.getId()==deed.getPosition().getId())
                {
                    avai=true;
                    break;
                }
            }
            if(!avai)
                return new ResponseEntity<>(JsonResp.badRequest("该职位归属他人或不存在"), HttpStatus.BAD_REQUEST);
        }

        //验证修改的事迹属于自己
        for(Deed deed1 : deedService.getDeedsByAId(archive.getId()))
        {
            if(deed1.getId()==deed.getId())
            {
                deedService.updateDeed(deed);
                archive.setDeeds(deedService.getDeedsByAId(archive.getId()));

                return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
            }
        }

        return new ResponseEntity<>(JsonResp.badRequest("该事迹归属他人或不存在"), HttpStatus.BAD_REQUEST);
    }

    /**
     * 删除事迹
     */
    @RequestMapping(value = "/deed/delete", method = RequestMethod.DELETE)
    ResponseEntity<JsonResp> putDeed(@RequestParam int deeid) throws Exception
    {
        Archive archive= AuthUtil.getCurUser().getArchive();
        if(archive==null)
            throw new Exception("尚未创建档案");

        //验证修改的事迹属于自己
        for(Deed deed : deedService.getDeedsByAId(archive.getId()))
        {
            if(deed.getId()==deeid)
            {
                deedService.deleteDeed(deeid);
                archive.setDeeds(deedService.getDeedsByAId(archive.getId()));

                return new ResponseEntity<>(JsonResp.ok("删除成功"), HttpStatus.OK);
            }
        }

        return new ResponseEntity<>(JsonResp.badRequest("该事迹归属他人或不存在"), HttpStatus.BAD_REQUEST);
    }


    /**
     * 新增技能
     */
    @RequestMapping(value = "/talent/post", method = RequestMethod.POST)
    ResponseEntity<JsonResp> postTalent(@RequestBody Talent talent) throws Exception
    {
        Archive archive=AuthUtil.getCurUser().getArchive();
        if(archive==null)
            throw new Exception("尚未创建档案");


        talentService.insertTalent(talent);
        archive.setTalents(talentService.getTalentByAId(archive.getId()));
        return new ResponseEntity<>(JsonResp.ok("新增成功","tid",talent.getId()), HttpStatus.OK);
    }

    /**
     * 修改技能 tooo
     */
    @RequestMapping(value = "/talent/put", method = RequestMethod.PUT)
    ResponseEntity<JsonResp> putTalent(@RequestBody Talent talent) throws Exception
    {
        Archive archive=AuthUtil.getCurUser().getArchive();
        if(archive==null)
            throw new Exception("尚未创建档案");

        boolean flag=false;
        for(Talent talent1: archive.getTalents())
        {
            if (talent1.getId()==talent.getId())
                flag=true;
        }
        if(!flag)
            throw new Exception("该技能归属他人或不存在");

        talentService.updateTalent(talent);
        archive.setTalents(talentService.getTalentByAId(archive.getId()));
        return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
    }

    @RequestMapping(value = "/talent/delete", method = RequestMethod.DELETE)
    ResponseEntity<JsonResp> deleteTalent(@RequestParam() int tid) throws Exception
    {
        Archive archive=AuthUtil.getCurUser().getArchive();
        if(archive==null)
            throw new Exception("尚未创建档案");

        boolean flag=false;
        for(Talent talent1: archive.getTalents())
        {
            if (talent1.getId()==tid)
                flag=true;
        }
        if(!flag)
            throw new Exception("该技能归属他人或不存在");
        talentService.deleteTalent(tid);
        archive.setTalents(talentService.getTalentByAId(archive.getId()));
        return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
    }

}
